package com.ics.atable.chat.prompt;

import lombok.extern.slf4j.Slf4j;
import org.springframework.util.StreamUtils;

import java.io.IOException;
import java.io.InputStream;
import java.nio.charset.StandardCharsets;
import java.util.concurrent.ConcurrentHashMap;

/**
 * Prompt loader, used to load prompt templates from file system
 *
 * @author haohongbin
 */
@Slf4j
public class PromptLoader {

    private static final String PROMPT_PATH_PREFIX = "prompts/";

    private static final ConcurrentHashMap<String, String> promptCache = new ConcurrentHashMap<>();

    /**
     * Load prompt template from file
     * @param promptName prompt file name (without path and extension)
     * @return prompt content
     */
    public static String loadPrompt(String promptName) {
        return promptCache.computeIfAbsent(promptName, name -> {
            String fileName = PROMPT_PATH_PREFIX + name + ".txt";
            try (InputStream inputStream = PromptLoader.class.getClassLoader().getResourceAsStream(fileName)) {
                if (inputStream == null) {
                    log.error("提示词文件不存在: {}", fileName);
                    throw new RuntimeException("提示词文件不存在: " + fileName);
                }
                String content = StreamUtils.copyToString(inputStream, StandardCharsets.UTF_8);
                log.debug("成功加载提示词: {}, 内容长度: {}", fileName, content.length());
                return content;
            }
            catch (IOException e) {
                log.error("加载提示词失败: {}", fileName, e);
                throw new RuntimeException("加载提示词失败: " + name, e);
            }
        });
    }

    /**
     * Clear prompt cache
     */
    public static void clearCache() {
        promptCache.clear();
    }

    /**
     * Get cache size
     * @return number of prompts in cache
     */
    public static int getCacheSize() {
        return promptCache.size();
    }
}
